<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true">
				<el-form-item>
					<el-input v-model="keyword" placeholder="姓名"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="keywordQuery">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd" disabled>新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="users" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="username" label="员工姓名" width="150" sortable>
			</el-table-column>
			<el-table-column prop="phone" label="电话" width="200" sortable>
			</el-table-column>
			<el-table-column prop="email" label="邮件" width="150" sortable>
			</el-table-column>
			<el-table-column prop="age" label="年龄" width="150" sortable>
			</el-table-column>
			<el-table-column prop="state" label="状态" min-width="150" sortable>
        <template scope="scope">
          <span v-if="scope.row.state==0" style="color: red">禁用</span>
          <span v-if="scope.row.state==1" style="color: green">启用</span>
        </template>
			</el-table-column>
<!--      <el-table-column prop="department.name" label="部门id" min-width="120" sortable>-->
<!--      </el-table-column>-->
<!--      <el-table-column prop="logininfo_id" label="登录信息id" min-width="120" sortable>-->
<!--      </el-table-column>-->
<!--      <el-table-column prop="shop_id" label="商店id" min-width="120" sortable>-->
<!--      </el-table-column>-->
			<el-table-column label="操作" width="150">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)" disabled>删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" disabled @click="batchRemove"  disabled>批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="pageSize" :current-page="currentPage" :total="totals" style="float:right;">
			</el-pagination>
		</el-col>

		<!--编辑界面-->
		<el-dialog :title=title :visible.sync="saveFormVisible" :close-on-click-modal="false">
			<el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
        <el-form-item label="状态">
          <el-radio-group v-model="saveForm.state">
            <el-radio class="radio" :label="1">启用</el-radio>
            <el-radio class="radio" :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="saveFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">提交</el-button>
			</div>
		</el-dialog>


	</section>
</template>

<script>
	// import util from '../../common/js/util'
	//import NProgress from 'nprogress'
	// import { getUserListPage, removeUser, batchRemoveUser, editUser, addUser } from '../../api/api';

	export default {
		data() {
			return {
        //员工列表
				users: [],
        //分页
				totals: 0,
        currentPage:1,
        pageSize:5,
        listLoading: false,
        //3.关键字查询
        keyword:"",

        title:"",
        // page: 1,
				sels: [],//列表选中列

        //  所有部门
        departments:[],

				saveFormVisible: false,//编辑界面是否显示
				saveLoading: false,
				saveFormRules: {
					username: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					]
				},
				//编辑界面数据
				saveForm: {
					id: null,
					username: '',
					phone:'',
          email:'',
					age: null,
					state:1,
          department_id:null,
				},




			}
		},
		methods: {
      keywordQuery(){
        this.currentPage=1;
        this.getUsers();
      },
      //点击下面分页栏进行页面切换
			handleCurrentChange(val) {
				this.currentPage = val;
				this.getUsers();
			},
			//获取用户列表
			getUsers() {
				let para = {
					pageSize:this.pageSize,
					currentPage:this.currentPage,
          keyword:this.keyword,
        };
        this.listLoading = true;
        this.$http.post("/user",para).then(res=>{
          this.users=res.data.data;
          this.totals=res.data.totals;
          this.listLoading=false;
        }).catch(res=>{
          this.$message.error("系统繁忙，请稍后询问");
        })
      },
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
				  //显示列表加载框
					this.listLoading = true;
					//注意前端发送的路径要和后端匹配
					this.$http.delete("/user/"+row.id).then(res=>{
					//  关闭列表加载框
            if(res.data.success){
              this.$message.success("删除成功");
              if((this.currentPage*this.pageSize-this.totals)>=this.pageSize-1){
                this.currentPage=this.currentPage-1;
              };
             // 查出
             var totalPage = Math.ceil(this.totals/this.pageSize);
              if(this.pageSize==totalPage&&(this.totals-1)%this.pageSize==0&&this.currentPage!=1){
                this.currentPage--;
              }
              this.getUsers();
            }else{
              this.$message.error("系统繁忙，请重新加载【500】");
            }
          }).catch(res=>{
            this.$message.error("删除失败，系统繁忙");
          })

				}).catch(() => {

				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
			  this.title="修改";
				this.saveFormVisible = true;
				this.saveForm = Object.assign({}, row);
				this.getUsers()
			},
			//显示新增界面
			handleAdd: function () {
			  this.title="新增";
				this.saveFormVisible = true;
				this.saveForm = {
          id: null,
          username: '',
          phone:'',
          email:'',
          age: null,
          state:1,
          department_id:null,
				};
				this.getDepartment();
			},
			//新增和提交
			saveSubmit: function () {
				this.$refs.saveForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
						  //显示保存加载框
              this.saveLoading = true;
              //获取表单的数据
              let para = Object.assign({}, this.saveForm);
							this.$http.put("/user",para).then(res=>{
							  this.saveFormVisible=false;
							  this.saveLoading=false;
							  if(res.data.success){
							    this.$message.success(this.title+"成功");
							    this.getUsers();
                }else {
							    this.$message.error("系统繁忙，请稍后再试！！【500】");
                }
              }).catch(res=>{
                this.$message.error("系统繁忙，请稍后再试[404]");
              })

						});
					}
				});
			},

			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id);
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					this.$http.patch("/user",ids).then(res=>{
					  this.listLoading=false;
					  if(res.data.success){
              this.$message.success("批量删除成功");
              this.getUsers();
            }else {
              this.$message.error("系统繁忙，请稍后再试！！【500】");
            }
          }).catch(res=>{
            this.$message.error("系统繁忙，请稍后再试");
          })

				}).catch(() => {

				});
			},

		},

		mounted() {
			this.getUsers();
		}
	}

</script>

<style scoped>

</style>